<template>
  <div class="permission-container">
    <div class="app-container">
      <el-card>
        <el-row type="flex" justify="end">
          <el-col :span="1">
            <el-button type="primary" size="small" @click="showAddDialog(1, '0')">添加权限</el-button>
          </el-col>
        </el-row>
        <!-- 表格 -->
        <el-table :data="permissionList" border stripe style="margin-top: 20px" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{row}">
              <!-- {{ row }} -->
              <el-button v-if="row.pid === '0'" @click="showAddDialog(2, row.id)">添加权限</el-button>
              <el-button @click="showEditDialog(row.id)">查看</el-button>
              <el-button @click="delRights(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <!-- 新增编辑的弹层 -->
      <!-- 新增编辑的弹层 -->
      <el-dialog :visible="showDialog" :title="title" @close="showDialog = false">
        <!-- 表单内容 -->
        <el-form label-width="100px">
          <el-form-item label="权限名称">
            <el-input v-model="formData.name" />
          </el-form-item>
          <el-form-item label="权限标识">
            <el-input v-model="formData.code" />
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input v-model="formData.description" />
          </el-form-item>
          <el-form-item label="权限启用">
            <el-switch
              v-model="formData.enVisible"
              active-text="启用"
              active-value="1"
              inactive-text="不启用"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>

        <template #footer>
          <div style="text-align: right;">
            <el-button @click="showDialog = false">取消</el-button>
            <el-button type="primary" @click="handleSubmit">确定</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { reqGetPermissionList, reqAddPermission, reqGetPermissionDetail, reqUpdatePermission, reqDelPermission } from '@/api/permission'
import { tranListToTreeData } from '@/utils/index'
export default {
  name: 'Permission',
  data() {
    return {
      permissionList: [], // 权限列表
      showDialog: false,
      formData: {
        enVisible: '0', // 开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型
        pid: '' // 添加到哪个节点下
      },
      title: '新增权限'
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    // 获取所有权限点
    async getPermissionList() {
      const { data } = await reqGetPermissionList()
      this.permissionList = tranListToTreeData(data, '0')
    },
    // 展示添加权限的对话框
    showAddDialog(type, pid) {
      // console.log(type, pid)
      this.showDialog = true
      this.formData.type = type
      this.formData.pid = pid
      this.title = '新增权限'
    },
    async handleSubmit() {
      // 拆分确定业务
      if (this.formData.id) {
        // 编辑
        await reqUpdatePermission(this.formData)
      } else {
        // 新增
        await reqAddPermission(this.formData)
      }
      // 1. 预校验
      // 2. 发送请求

      // 3. 关闭对话框
      this.showDialog = false
      // 4. 重新获取权限点
      this.getPermissionList()
      // 5. 清空输入框
      this.formData = {
        enVisible: '0', // 开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型
        pid: '' // 添加到哪个节点下
      }
    },
    // 点击查看显示对话框
    async showEditDialog(id) {
      this.title = '编辑权限'
      // console.log(id)
      // 1. 根据id获取权限点信息
      const { data } = await reqGetPermissionDetail(id)
      console.log(data)
      // 2. 将权限点信息赋值给formData
      this.formData = data
      // 3. 展示对话框
      this.showDialog = true
    },
    // 删除权限
    async delRights(id) {
      // 1.确认消息框
      const str = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        type: 'warning'
      }).catch(() => {})
      if (str !== 'confirm') return this.$message.info('已取消')
      // 2. 发送请求
      await reqDelPermission(id)
      // 3. 重新渲染
      this.getPermissionList()
    }
  }
}
</script>

<style>

</style>
